/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.ClaimType;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import sessionBeans.ClaimTypeFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class ClaimTypeManagement implements Serializable {

    @EJB
    private ClaimTypeFacadeLocal claimTypeFacade;
    private ClaimType claimType = new ClaimType();
    private ClaimType selectClaimType = new ClaimType();
    private List<ClaimType> list;
    private List<ClaimType> filteredList;

    public void create() {
        if (!validate(claimType)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            claimTypeFacade.create(claimType);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create ", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Create ", e.getMessage() + " Dupldate Name");
        }
        claimType = new ClaimType();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit(ActionEvent event) {
        boolean frmEditClaimType = event.getComponent().getClientId().contains("frmEditClaimType");
        if (!frmEditClaimType) {
            if (!validate(selectClaimType)) {
                return;
            }
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            claimTypeFacade.edit(selectClaimType);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit ", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public ClaimType getClaimType() {
        return claimType;
    }

    public void setClaimType(ClaimType claimType) {
        this.claimType = claimType;
    }

    public ClaimType getSelectClaimType() {
        return selectClaimType;
    }

    public void setSelectClaimType(ClaimType selectClaimType) {
        this.selectClaimType = selectClaimType;
    }

    public List<ClaimType> getList() {
        list = claimTypeFacade.findAll();
        return list;
    }

    public void setList(List<ClaimType> list) {
        this.list = list;
    }

    public List<ClaimType> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<ClaimType> filteredList) {
        this.filteredList = filteredList;
    }

    //Validate ClaimType
    public boolean validate(ClaimType claimtype) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (claimtype.getClaimTypeName() == null || claimtype.getClaimTypeName().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeName is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (claimtype.getClaimTypeName().length() < 2 || claimtype.getClaimTypeName().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeName is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(claimtype.getClaimTypeName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeName is only Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (claimtype.getClaimTypeDescription() == null || claimtype.getClaimTypeDescription().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeDescription is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (claimtype.getClaimTypeDescription().length() < 2 || claimtype.getClaimTypeDescription().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeDescription is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(claimtype.getClaimTypeDescription())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ClaimTypeDescription is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
